package com.itheima.controller;

import com.itheima.Result.Result;
import com.itheima.utils.AliOSSUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequiredArgsConstructor
@Slf4j
@Api(tags = "上传文件")
public class UploadController {

    private final AliOSSUtils aliOSSUtils;

    /**
     * 上传文件
     *
     * @return
     */
    @ApiOperation(value="上传图片",notes="上传图片到阿里云oss存储空间中")
    @PostMapping("/upload")
    public Result uploadFile(MultipartFile image) {
        try {
            return Result.success(aliOSSUtils.uploadFile(image));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.error("上传失败");
    }
}
